<?php
defined( '_VALID_MOS' ) or die( 'Restricted access' );

class JEConfig
{
    function get($val, $option='')
    {
        global $database;
        if(!$option) $option = mosGetParam( $_REQUEST,'option', false);
        if(!$option || !$val) return false;
        $val = explode('.', $val);
        $section = $val[0];
        $value   = $val[1];
        $sql = "SELECT `selected` FROM #__je_config WHERE `section` = '{$section}' AND name = '{$value}'";
        $database->setQuery( $sql );
        $rows = $database->loadResult();
        return $rows;
    }
    
    function renderForm($form = false)
    {
        global $database;
?>


		<table class="adminheading">
		<tr>
			<th class="config">
			Configuration
			</th>
		</tr>
		</table>
<?php
        
        
        
        
        if($form) echo '<form action="index2.php" method="post" id="adminForm" name="adminForm" enctype="">';
        //echo "<br>";
        $option = mosGetParam( $_REQUEST,'option', false);
        if(!$option) josRedirect( 'index2.php', JText::_('Cannot get Component name in JEConfig') );
        
        $sql = "SELECT name FROM #__je_config WHERE component = '{$option}' AND hidden = 'N'  ORDER BY `section` ASC, fieldset ASC, type  ASC ";
        $database->setQuery( $sql );
        $rows = $database->loadAssocList();
        
        $sql = "SELECT component FROM #__jp_pages GROUP BY component";
        $database->setQuery( $sql );
        $componentList= $database->loadObjectList();
        
		
        foreach ($componentList as $component){
		$find = false;
			foreach($rows as $row){
				if($row['name'] == $component->component){
					$find = true;
				}
			}
						
			if(!$find)
        	{
				$sql = "INSERT INTO `jos_je_config` VALUES ('', 'com_jp', 'Component', '".$component->component."', '".$component->component."', 'yesno', 'Exclusion Component', '', '0', 'N', 'Exclusion Component')";
        		$database->setQuery( $sql );
        		$database->query();
				
        	}
        }
        
        $sql = "SELECT * FROM #__je_config WHERE component = '{$option}' AND hidden = 'N'  ORDER BY `section` ASC, fieldset ASC, type  ASC ";
        $database->setQuery( $sql );
        $rows = $database->loadObjectList();
        $com = '';
        
        $tabs = new mosTabs(1);
        $tabs->startPane('config');

/*
        foreach($rows AS $k => $row)
        {
            if($com != $row->section)
            {
              if($com != '') 
              {
                  echo '</table>';
                  $tabs->endTab();
                  
              }
              $tabs->startTab(ucfirst($row->section), 'tab-'.$row->section);
              echo '<table class="adminform">';
            }
            $com = $row->section;
            echo self::_renderRow($row);
        }
*/        
        $set = '';
        foreach($rows AS $k => $row)
        {
            if($set != $row->fieldset)
            {
                if(! empty($set)) 
                {
                    echo '</table></fieldset>';
                }
            }
            if($com != $row->section)
            {
                if($com != '') 
                {
                    $tabs->endTab();
                }
                $tabs->startTab(ucfirst($row->section), 'tab-'.$row->section);
            }
            if($set != $row->fieldset)
            {
                //if($set != '') 
                //{
                //   $tabs .= '</fieldset>';
                //}
                echo '<fieldset style="display:block; width:5%; float:left; clear:both"><LEGEND>'.ucfirst($row->fieldset).'</LEGEND><table width="20%" class="admintable">';
            }
            $com = $row->section;
            $set = $row->fieldset;
            //$tabs .= "<P>Get it</P>";
            echo JEConfig::_renderRow($row);
        }
        echo '</table></fieldset>';
        $tabs->endTab();
        $tabs->endPane();
        if($form)
        {
            echo '<input type="hidden" name="option" value="com_ftpsearch" />';
            echo '<input type="hidden" name="task" value="" />';
            echo '<input type="hidden" name="hidemainmenu" value="0" />';
            echo '</form>';
        } 
    }
    
    function _renderRow($row)
    {
        $label = $row->lable;
        if($row->description)
        {
            $label = sprintf(
                '<SPAN class="editlinktip"><SPAN onmouseover="return '.
                'overlib(\'%s\', BELOW, RIGHT, WIDTH, \'280\');" '.
                'onmouseout="return nd();" style="text-decoration: '.
                'none; color: #333;">%s</SPAN></SPAN>',$row->description ,$row->lable);
        }
        $out = sprintf('<tr valign="top"><td width="10%s" nowrap="nowrap">%s:</td><td width="10%s" nowrap="nowrap">%s</td></tr>', '%', $label, '%',  JEConfig::_renderElement($row));
        return $out;
    }
    
    function _renderElement($row)
    {
        switch ($row->type)
        {
            case "yesno":
                $out = mosHTML::yesnoRadioList($row->name.$row->id, 'class="inputbox"', $row->selected);
            break;
            
            case "selectlist":
                $items = explode(",", $row->values);
                foreach($items AS $item)
                {
                    $list[] = mosHTML::makeOption(trim($item), trim($item));
                }
                $out = mosHTML::selectList($list, $row->name.$row->id, 'class="inputbox" size="'.count($items).'" ', 'value', 'text', $row->selected);
            break;
            
            case "dropdown":
                $items = explode(",", $row->values);
                foreach($items AS $item)
                {
                    $list[] = mosHTML::makeOption(trim($item), trim($item));
                }
                $out = mosHTML::selectList($list, $row->name.$row->id, 'class="inputbox" size="1" ', 'value', 'text', $row->selected);
            break;
            
            case "textarea":
                $out = sprintf('<textarea class="text_area" name="%s" rows="5" cols="40">%s</textarea>', $row->name.$row->id, $row->selected);
            break;
            
            default:
                $out = sprintf('<input type="text" class="inputbox" name="%s" value="%s" size="40">', $row->name.$row->id, $row->selected);
            break;
        }
        
        return $out;
    }

    function Toolbar()
    {
        mosMenuBar::startTable();
    		//mosMenuBar::title( JText::_( 'JPromoter [Edit Page]'), '../components/com_jp/images/logo.png' );
    		//mosMenuBar::spacer();
        //mosMenuBar::custom('panel', '../components/com_jp/images/cpanel.png', NULL, JText::_('cPanel'), false, false );
    		//mosMenuBar::spacer();
    		//mosMenuBar::divider();
    		mosMenuBar::spacer();

        mosMenuBar::save('savecnf');
    		mosMenuBar::spacer();

        mosMenuBar::apply('applycnf');
    		mosMenuBar::spacer();

        mosMenuBar::cancel();
    		mosMenuBar::spacer();


    		//mosMenuBar::help('index.html', 'com_jp');
    		mosMenuBar::endTable();
    }
    
    function save($option = '')
    {
        global $database;
        
        if(!$option) $option = mosGetParam( $_REQUEST,'option', false);
        if(!$option) 
        {
            echo "Cannot save. No Option";
            return;
        }
        $sql = "SELECT * FROM #__je_config WHERE `component` = '{$option}' AND hidden = 'N'";
        //echo $sql;
        $database->setQuery( $sql );
        $rows = $database->loadObjectList();
        foreach($rows AS $row)
        {
            $val = addslashes(mosGetParam( $_REQUEST, $row->name.$row->id));
            $sql = "UPDATE #__je_config SET selected = '$val' WHERE `id` = '{$row->id}'";
            $database->setQuery( $sql );
            $database->query();
        }
    }
}

?>